/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxPathSum = function(root) {
    let maxSum = Number.MIN_SAFE_INTEGER
    const dfs = (root) => {
        if(root == null) return 0
        const left = dfs(root.left)
        const right = dfs(root.right)
        const innerMaxSum = left + right +  root.val
        // 如果节点全是负数，其实就是找最大值，这里会处理
        maxSum = Math.max(maxSum, innerMaxSum)
        const outputMaxSum = root.val + Math.max(0, left, right)
        return outputMaxSum < 0 ? 0 : outputMaxSum
    }
    dfs(root)
    return maxSum
};